package com.harry.service;

import com.harry.mapper.UserMapper;
import com.harry.pojo.Doctor;
import com.harry.pojo.GetMedicine;
import com.harry.pojo.RegisterRecord;
import com.harry.pojo.ScheduleRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserService
{
    @Resource
    private UserMapper userMapper;

    public String login(String open_id)
    {
        if(userMapper.login(open_id) != null)
        {
            return String.valueOf(userMapper.login(open_id).getRole());
        }
        else
            return "no";
    }

    public List<Doctor> getDoctors()
    {
        return userMapper.getDoctors();
    }

    public String register(int doctor_id, String open_id, Timestamp start_time)
    {
        ScheduleRecord  scheduleRecord = userMapper.getDoctorAvailable(doctor_id, start_time);
        if(scheduleRecord.getRegister_limit() == scheduleRecord.getRegister_num())
        {
            return "fail";
        }
        else
        {
            userMapper.addRegister(open_id, doctor_id, start_time);
            userMapper.updateDoctorRegister(doctor_id, start_time);
            return "success";
        }
    }

    public String payment(String open_id)
    {
        return userMapper.payment(open_id).getPayment_info();
    }

    public List<String> getMedicine(String open_id)
    {
        List<GetMedicine> getMedicineList = userMapper.getMedicine(open_id);

        List<String> results = new ArrayList<>();
        if(getMedicineList.isEmpty())
        {
            results.add("No medicine needed");
            return results;
        }
        for(GetMedicine getMedicine : getMedicineList)
        {
            if(userMapper.getSingleMedicineRepo(getMedicine.getMedicine_id()).getQuantity() == 0)
            {
                results.add("No " + userMapper.getSingleMedicineRepo(getMedicine.getMedicine_id()).getMedicine_name() + " left in repository");
            }
            else
                results.add(getMedicine.getMedicine_info());
        }
        return results;
    }

    public int getNowStage(String open_id)
    {
        return userMapper.getNowStage(open_id).getStage();
    }

    public List<RegisterRecord> getRegisterRecord(String open_id)
    {
        return userMapper.getRegisterRecord(open_id);
    }
}
